import os

import spiders
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
from multiprocessing import Process

from app import app
from db.dao import IndustryGrowthListDao

path = os.path.dirname(spiders.__file__)
os.chdir(path)


def market():
    '''
    大盘/行业资金
    :return:
    '''
    crawler1 = CrawlerProcess(get_project_settings())
    crawler1.crawl('stock_market')
    # crawler1.crawl('stock_funds_2', stock_list)
    oldPath = os.getcwd()
    # ctx = app.app_context()
    # ctx.push()
    crawler1.start(stop_after_crawl=True)
    os.chdir(oldPath)


def industry():
    crawler1 = CrawlerProcess(get_project_settings())
    crawler1.crawl('sector_quotation')
    # crawler1.crawl('stock_funds_2', stock_list)
    oldPath = os.getcwd()
    # ctx = app.app_context()
    # ctx.push()
    crawler1.start(stop_after_crawl=True)
    os.chdir(oldPath)


def stockInfo(*industry_list):
    crawler1 = CrawlerProcess(get_project_settings())
    crawler1.crawl('stock_info', industry_list)
    oldPath = os.getcwd()
    # ctx = app.app_context()
    # ctx.push()
    crawler1.start(stop_after_crawl=True)
    os.chdir(oldPath)


def stockInfoSingle(*start_url):
    crawler1 = CrawlerProcess(get_project_settings())
    crawler1.crawl('stock_info_single', start_url)
    oldPath = os.getcwd()
    # ctx = app.app_context()
    # ctx.push()
    crawler1.start(stop_after_crawl=True)
    os.chdir(oldPath)


def market_industry():
    '''
     大盘/行业资金
     :return:
     '''
    p = Process(target=market)
    p1 = Process(target=industry)
    p.start()
    p1.start()
    p.join()
    p1.join()


def stock_funds_1(*stock_list):
    crawler1 = CrawlerProcess(get_project_settings())
    crawler1.crawl('stock_funds_1', stock_list)

    oldPath = os.getcwd()
    # ctx = app.app_context()
    # ctx.push()
    crawler1.start(stop_after_crawl=True)
    os.chdir(oldPath)


def stock_funds_2(*stock_list):
    crawler1 = CrawlerProcess(get_project_settings())
    crawler1.crawl('stock_funds_2', stock_list)
    oldPath = os.getcwd()
    # ctx = app.app_context()
    # ctx.push()
    crawler1.start(stop_after_crawl=True)
    os.chdir(oldPath)

def stock_funds_holder(*stock_list):
    crawler1 = CrawlerProcess(get_project_settings())
    crawler1.crawl('stock_funds_holder', stock_list)
    oldPath = os.getcwd()
    ctx = app.app_context()
    ctx.push()
    crawler1.start(stop_after_crawl=True)
    os.chdir(oldPath)



def importIndustryInfo():
    crawler1 = CrawlerProcess(get_project_settings())
    crawler1.crawl('industry_info')
    oldPath = os.getcwd()
    # ctx = app.app_context()
    # ctx.push()
    crawler1.start(stop_after_crawl=True)
    os.chdir(oldPath)


def importStockFundamental(*stock_list):
    crawler1 = CrawlerProcess(get_project_settings())
    crawler1.crawl('stock_fundamental', stock_list)
    oldPath = os.getcwd()
    ctx = app.app_context()
    ctx.push()
    crawler1.start(stop_after_crawl=True)
    os.chdir(oldPath)


def importIndustryGuid():
    '''
    行业导读
    :return:
    '''
    crawler1 = CrawlerProcess(get_project_settings())
    crawler1.crawl('news_industry_guide')
    oldPath = os.getcwd()
    ctx = app.app_context()
    ctx.push()
    crawler1.start(stop_after_crawl=True)
    os.chdir(oldPath)


def importIndustryResearch():
    '''
    行业研究
    :return:
    '''
    crawler1 = CrawlerProcess(get_project_settings())
    crawler1.crawl('news_industry_research')
    oldPath = os.getcwd()
    # ctx = app.app_context()
    # ctx.push()
    crawler1.start(stop_after_crawl=True)
    os.chdir(oldPath)


def importIndustryInformation():
    '''
    行业研究
    :return:
    '''
    crawler1 = CrawlerProcess(get_project_settings())
    crawler1.crawl('news_industry_information')
    oldPath = os.getcwd()
    # ctx = app.app_context()
    # ctx.push()
    crawler1.start(stop_after_crawl=True)
    os.chdir(oldPath)


def getWorkday():
    '''
    股票排行版
    :return:
    '''
    crawler1 = CrawlerProcess(get_project_settings())
    crawler1.crawl('market_workday')
    oldPath = os.getcwd()
    ctx = app.app_context()
    ctx.push()
    crawler1.start(stop_after_crawl=True)
    os.chdir(oldPath)



